package gov.pbc.nn.kjgl.software.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.utils.EntityRowMapperUtils;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.entity.Project;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Repository
public class ProjectBackupJdbcDao extends AbstractJdbcDao {

	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hiberanteDao;

	public void getPageJdbcAll(Page<Project> page, Criteria criteria) {

		// String
		// user="SELECT * from  t_project p INNER JOIN t_project_member  m WHERE  p.ID_=m.PROJECT_ID_ and m.USER_NAME_=:";
		// String Username=ContextHolder.getLoginUserName();
		int LoginUserLevel = getByUsername(ContextHolder.getLoginUserName());

		String sql = "from Project";
		List<Project> query = hiberanteDao.query(sql);
		List<String> Plist = new ArrayList<String>();
		for (Project p : query) {
			boolean isInMember = isInMember(ContextHolder.getLoginUserName(), p.getId());
			if (!isInMember) {
				/*int pLevel = getByUsername(p.getCreateUser());
				if (LoginUserLevel == 3 && pLevel == 3) {
					Plist.add(p.getId());
					continue;
				} else if (LoginUserLevel == 2 && (pLevel == 2 || pLevel == 3)) {
					Plist.add(p.getId());
					continue;
				} else if (LoginUserLevel == 1 && (pLevel == 1 || pLevel == 2 || pLevel == 3)) {
					Plist.add(p.getId());
					continue;
				} else if (LoginUserLevel == 0) {
					Plist.add(p.getId());
					continue;
				}
				*/
				Dept loginDept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
				Dept loginZZDept = (Dept) SecurityUtils.getZZByDept(loginDept);
				if (Dept.NANNING.equals(loginZZDept.getId())&&(p.getProjectDept().startsWith(Dept.NANNING)||p.getState()==10)) {
					Plist.add(p.getId());
				}
				else if(SecurityUtils.getZZByDept(p.getProjectDept()).getId().equals(loginZZDept.getId())&&(p.getState()==10||p.getState()==25)){
					Plist.add(p.getId());
				}
			} else {
				Plist.add(p.getId());
			}

		}

		String tempId = "'s',";
		for (String s : Plist) {
			tempId = tempId + "'" + s + "',";
		}
		if (StringUtils.isNotEmpty(tempId)) {
			tempId = tempId.substring(0, tempId.length() - 1);
		}

		String sql1 = "select * from t_project where INVALID_=0 and ID_ in( " + tempId + ")";
		Map<String, Object> params = new HashMap<String, Object>();
		// params.put("tempId", tempId);
		ParseResult pr = this.parseCriteria(criteria, true, "T");
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql1 += " and " + sbSql.toString() + " order by CREATE_DATE_ desc";
			params.putAll(pr.getValueMap());
		} else {
			sql1 += " order by CREATE_DATE_ desc";
		}

		this.pagingQuery(page, sql1, new RowMapper<Project>() {

			@Override
			public Project mapRow(ResultSet rs, int rowNum) throws SQLException {
				return EntityRowMapperUtils.get(Project.class, rs, rowNum);
			}
		}, params);

	}

	// 根据用户名返回用户部门层级
	public int getByUsername(String username) {
		IDeptService ds = ContextHolder.getBean(IDeptService.BEAN_ID);
		Dept dept = (Dept) ds.loadUserDepts(username).get(0);
		return dept.getLevel();
	}

	public boolean isInMember(String username, String id) {
		String sql = "SELECT count(*) as mycount from  t_project_member  m WHERE m.INVALID_=0 and m.PROJECT_ID_ =? and m.USER_NAME_=? ";
		// int queryForInt = hiberanteDao.queryForInt(sql);
		Map<String, Object> rs = this.getJdbcTemplate().queryForMap(sql, new Object[]{id,username});
		if ((Long)rs.get("mycount") > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isInkjMember(String username, String id) {
		String sql = "SELECT count(*) as mycount from  t_project_member  m WHERE m.INVALID_=0 and m.PROJECT_ID_ =? and m.USER_NAME_=? and m.PROJECT_ROLE_ in (?,?,?)";
		// int queryForInt = hiberanteDao.queryForInt(sql);
		Map<String, Object> rs = this.getJdbcTemplate().queryForMap(sql, new Object[]{id,username,0,1,2});
		if ((Long)rs.get("mycount") > 0) {
			return true;
		} else {
			return false;
		}
	}
}
